A async interface for diesel
Diesel gets rid of the boilerplate for database interaction and eliminates runtime errors without sacrificing performance. It takes full advantage of Rust's type system to create a low overhead query builder that "feels like Rust."
Diesel-async provides an async implementation of diesels connection implementation and any method that may issue an query. It is designed as pure async drop-in replacement for the corresponding diesel methods. Similar to diesel the crate is designed in a way that allows third party crates to extend the existing infrastructure and even provide their own connection implementations.
Supported databases:
- PostgreSQL
- MySQL
Usage
Simple usage
Diesel-async is designed to work in combination with diesel, not to replace diesel. For this it provides drop-in replacement for diesel functionality that actually interacts with the database.
A normal project should use a setup similar to the following one:
[]
= "2.1.0" # no backend features need to be enabled
= { = "0.3.1", = ["postgres"] }
This allows to import the relevant traits from both crates:
use *;
use ;
// ordinary diesel model setup
table!
// create an async connection
let mut connection = establish.await?;
// use ordinary diesel query dsl to construct your query
let data: = table
.filter
.or_filter
.select
// execute the query via the provided
// async `diesel_async::RunQueryDsl`
.load
.await?;
Async Transaction Support
Diesel-async provides an ergonomic interface to wrap several statements into a shared database transaction. Such transactions are automatically rolled back as soon as the inner closure returns an error
connection..await?;
Streaming Query Support
Beside loading data directly into a vector, diesel-async also supports returning a value stream for each query. This allows to process data from the database while they are still received.
// use ordinary diesel query dsl to construct your query
let data:
Built-in Connection Pooling Support
Diesel-async provides built-in support for several connection pooling crates. This includes support for:
Deadpool
use AsyncDieselConnectionManager;
use Pool;
use RunQueryDsl;
// create a new connection pool with the default config
let config = new;
let pool = builder.build?;
// checkout a connection from the pool
let mut conn = pool.get.await?;
// use the connection as ordinary diesel-async connection
let res = table.select..await?;
BB8
use AsyncDieselConnectionManager;
use Pool;
use RunQueryDsl;
// create a new connection pool with the default config
let config = new;
let pool = builder.build.await?;
// checkout a connection from the pool
let mut conn = pool.get.await?;
// use the connection as ordinary diesel-async connection
let res = table.select..await?;
Mobc
use AsyncDieselConnectionManager;
use Pool;
use RunQueryDsl;
// create a new connection pool with the default config
let config = new;
let pool = new;
// checkout a connection from the pool
let mut conn = pool.get.await?;
// use the connection as ordinary diesel-async connection
let res = table.select..await?;
Crate Feature Flags
Diesel-async offers several configurable features:
postgres
: Enables the implementation ofAsyncPgConnection
mysql
: Enables the implementation ofAsyncMysqlConnection
deadpool
: Enables support for thedeadpool
connection pool implementationbb8
: Enables support for thebb8
connection pool implementationmobc
: Enables support for themobc
connection pool implementation
By default no features are enabled.
Code of conduct
Anyone who interacts with Diesel in any space, including but not limited to this GitHub repository, must follow our code of conduct.
License
Licensed under either of these:
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
Contributing
Contributions are explicitly welcome. Please consider opening a discussion with your idea first, to discuss possible designs.
Unless you explicitly state otherwise, any contribution you intentionally submit for inclusion in the work, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.